import numpy as np
import matplotlib.pyplot as plt


def bezier_curve(control_points, num_points):
    t = np.linspace(0, 1, num_points)
    n = len(control_points) - 1
    curve_points = []

    for i in range(num_points):
        point = np.zeros(2)
        for j in range(n + 1):
            point += binomial_coefficient(n, j) * (1 - t[i]) ** (n - j) * t[i] ** j * control_points[j]
        curve_points.append(point)

    return np.array(curve_points)


def binomial_coefficient(n, k):
    return np.math.factorial(n) / (np.math.factorial(k) * np.math.factorial(n - k))


# 定义控制点的坐标
control_points = np.array([(1, 1), (2, 4), (4, 2), (5, 6)])

# 拟合曲线上的点数
num_points = 1000

# 计算曲线上的点
curve_points = bezier_curve(control_points, num_points)

# 绘制控制点和曲线
plt.plot(control_points[:, 0], control_points[:, 1], 'ro-', label='Control Points')
plt.plot(curve_points[:, 0], curve_points[:, 1], 'b-', label='Bezier Curve')
plt.legend()
plt.show()